package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequiredLogin;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.TravelComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.queryObject.TravelQueryObject;
import cn.wolfcode.wolf2w.service.ITravelCommentService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.util.UMEditorUploader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("travels")
public class TravelController {

    @Autowired
    private ITravelService travelService;
    @Autowired
    private ITravelCommentService travelCommentService;

    /**
     * 分页
     */
    @GetMapping("/query")
    @ResponseBody
    public Object query(TravelQueryObject qo){
        return JsonResult.success(travelService.query(qo));
    }

    //上传图片
    @RequestMapping("/contentImage")
    @ResponseBody
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception{
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\""+ up.getFileName() +"\", \"originalName\": \""+ up.getOriginalName() +"\", \"size\": "+ up.getSize()
                +", \"state\": \""+ up.getState() +"\", \"type\": \""+ up.getType() +"\", \"url\": \""+ up.getUrl() +"\"}";
        result = result.replaceAll( "\\\\", "\\\\" );
        if(callback == null ){
            return result ;
        }else{
            return "<script>"+ callback +"(" + result + ")</script>";
        }
    }

    /**
     * 游记明细
     * @param id
     * @return
     */
    @GetMapping("/detail")
    @ResponseBody
    public Object detail(String id){
        return JsonResult.success(travelService.get(id));
    }

    /**
     * 游记评论 , 不分页
     * @param travelId
     * @return
     */
    @GetMapping("/comments")
    @ResponseBody
    public Object comments(String travelId){
        List<TravelComment> travelComments = travelCommentService.queryByTravelId(travelId);
        return JsonResult.success(travelComments);
    }

    @GetMapping("/get")
    @ResponseBody
    public Object get(String id){
        if (StringUtils.hasLength(id)){
            Travel travel = travelService.get(id);
            return JsonResult.success(travel);
        }
        return JsonResult.success();
    }

    /**
     * 游记的新增和编辑
     * 只有已经登录了的用户才能进行编辑和修改,且只能修改自己编写的游记
     * @param travel
     * @return
     */
    @RequiredLogin
    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Travel travel,@UserParam UserInfo userInfo){

        if (!travel.getUserId().equals(userInfo.getId()) && userInfo==null){
            return JsonResult.error(JsonResult.CODE_ERROR, "这不是您的游记,无法进行编辑", null);
        }

        // 如果用户对象不为空则已登录可以进行修改和编辑
        if (userInfo != null) {
            travel.setUserId(userInfo.getId());
            travelService.saveOrUpdate(travel);
            return JsonResult.success(travel.getId());
        }

        // 如果用户对象为空则表示没有登录
        return JsonResult.noLogin();

    }

    @GetMapping("/info")
    public Object info(@UserParam UserInfo userInfo){
        return JsonResult.success(userInfo);
    }


    /**
     * 游记评论添加
     * @return
     */
    @PostMapping("/commentAdd")
    @ResponseBody
    @RequiredLogin // 登录了才可以进行评论
    public Object commentAdd(TravelComment travelComment, @UserParam UserInfo userInfo){

            //使用登录用户参数解析器来传入当前用户对象
        travelCommentService.save(travelComment,userInfo);

        return JsonResult.success();
    }
}
